package com.rovinj_server.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.rovinj_server.R;
import com.rovinj_server.entity.InventoryTaskSub;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SameAdapter extends BaseAdapter {

    private Context mContext;
    public int showCount = 20;
    private List<InventoryTaskSub> mList = new ArrayList<>();
    private HashMap<Integer, Boolean> isSelected;

    public SameAdapter(Context context, List<InventoryTaskSub> mList) {
        this.mContext = context;
        this.mList = mList;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < mList.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
//		return mList.size();
        if (mList != null && mList.size() > 0) {
            return showCount > mList.size() ? mList.size() : showCount;
        } else {
            return mList==null?0:mList.size();
        }

    }

    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewgroup) {
        List<InventoryTaskSub> list = new ArrayList<>();
        if (mList.size() > showCount) {
            list = mList.subList(0, showCount);
        } else {
            list = mList;
        }
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {

            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_same, viewgroup, false);
            holder.Location = (TextView) convertView.findViewById(R.id.Location);
            holder.barode = (TextView) convertView.findViewById(R.id.barode);
            holder.Sjnumber = (TextView) convertView.findViewById(R.id.Sjnumber);
            holder.diffNumber = (TextView) convertView.findViewById(R.id.diffNumber);
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb_same);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        }
        InventoryTaskSub inventoryTaskSub = mList.get(i);
        holder.Location.setText(inventoryTaskSub.getLocation());
        holder.barode.setText(inventoryTaskSub.getBarode());
        holder.Sjnumber.setText(inventoryTaskSub.getSjnumber());
        holder.diffNumber.setText(inventoryTaskSub.getDiscNumber() + "");
        holder.cb.setChecked(getIsSelected().get(i));
        return convertView;
    }

    public void setList(List<InventoryTaskSub> list) {
        mList = list;
    }

    public static class ViewHolder {

        public TextView Location;
        public TextView barode;
        public TextView diffNumber;
        public TextView Sjnumber;
        public CheckBox cb;
    }
}
